﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIDesigner.SurfaceProperties;
using System.Diagnostics;
using UIDesigner.DesignContainers.Bases;
using AFSharedLibrary;
using System.Reflection;
using DDW;

namespace UIDesigner.Designers.Forms
{
    public partial class frmSurfaceState : Form
    {
        private class ListItem
        {
            public Type Type;
            public string Name;

            public ListItem(Type objType, string strName)
            {
                Type = objType;
                Name = strName;
            }

            public override string ToString()
            {
                return Name;
            }
        }

        private SurfaceStateBase m_objSurfaceStateBase;

        public frmSurfaceState(SurfaceStateBase objSurfaceStateBase)
        {
            InitializeComponent();
            m_objSurfaceStateBase = objSurfaceStateBase;

            this.Text = m_objSurfaceStateBase.Name;
        }

        private SurfacePropertiesBase SurfaceProperties
        {
            get
            {
                return m_objSurfaceStateBase.Host.SurfacePropertiesSet[m_objSurfaceStateBase.BaseObjectControl];
            }
        }

        private IEventStateValue EventStateValue
        {
            get
            {
                switch (m_objSurfaceStateBase.KindType.Name)
                {
                    case "SurfaceBindPropertyKind":
                        return SurfaceProperties.BindProperties.BindState; 
                    case "SurfaceCreatePropertyKind":
                        return SurfaceProperties.CreateProperties.CreateState; 
                    case "SurfaceDeletePropertyKind":
                        return SurfaceProperties.DeleteProperties.DeleteState;
                    case "SurfaceSavePropertyKind":
                        return SurfaceProperties.SaveProperties.SaveState;
                    default:
                        Debugger.Break();
                        return null;
                }
            }
        }

        private AFDesignerHost Host
        {
            get
            {
                return m_objSurfaceStateBase.Host;
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private Dictionary<string, IComponent> Components
        {
            get
            {
                Dictionary<string, IComponent> rgComponents = new Dictionary<string, IComponent>();

                foreach (IComponent objComponent in ((IContainer)this.Host).Components)
                {
                    IBaseObjectControl objBaseObjectControl = objComponent as IBaseObjectControl;

                    if (objBaseObjectControl == null)
                    {
                        if (!rgComponents.ContainsKey(objComponent.Site.Name))
                        {
                            rgComponents.Add(objComponent.Site.Name, objComponent);
                        }
                    }
                }

                return rgComponents;
            }
        }

        private Dictionary<string, Construct> Constructs
        {
            get
            {
                Dictionary<string, Construct> rgConstructs = new Dictionary<string, Construct>();

                foreach (IComponent objComponent in ((IContainer)this.Host).Components)
                {
                    IBaseObjectControl objBaseObjectControl = objComponent as IBaseObjectControl;

                    if (objBaseObjectControl != null)
                    {
                        Construct objConstruct = objBaseObjectControl.BusinessObject;

                        if (!rgConstructs.ContainsKey(objConstruct.Name))
                        {
                            rgConstructs.Add(objConstruct.Name, objConstruct);
                        }
                    }
                }

                return rgConstructs;
            }
        }

        private void frmSurfaceState_Load(object sender, EventArgs e)
        {
            if (this.SurfaceProperties is FormSurfaceProperties)
            {
                cboEventObject.Items.Add(new ListItem(typeof(Form), typeof(Form).Name));
                cboStateObject.Items.Add(new ListItem(typeof(Form), typeof(Form).Name));  
            }
            else if (this.SurfaceProperties is ControlSurfaceProperties)
            {
                cboEventObject.Items.Add(new ListItem(typeof(UserControl), typeof(UserControl).Name));
                cboStateObject.Items.Add(new ListItem(typeof(UserControl), typeof(UserControl).Name));
            }

            foreach (Construct objConstruct in this.Constructs.Values)
            {
                cboEventObject.Items.Add(new ListItem(typeof(Construct), objConstruct.Name));
                cboStateObject.Items.Add(new ListItem(typeof(Construct), objConstruct.Name));
            }

            foreach (IComponent objComponent in this.Components.Values)
            {
                cboEventObject.Items.Add(new ListItem(typeof(IComponent), objComponent.Site.Name));
                cboStateObject.Items.Add(new ListItem(typeof(Construct), objComponent.Site.Name));
            }
        }

        private void cboEventObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem objItem = (ListItem) cboEventObject.SelectedItem;

            cboEvent.Items.Clear();
            cboEvent.Enabled = true;

            if (objItem.Type.Name == typeof(Form).Name)
            {
                foreach (EventInfo objEvent in typeof(Form).GetEvents())
                {
                    cboEvent.Items.Add(new ListItem(typeof(EventInfo), objEvent.Name));
                }
            }
            else if (objItem.Type.Name == typeof(UserControl).Name)
            {
                foreach (EventInfo objEvent in typeof(UserControl).GetEvents())
                {
                    cboEvent.Items.Add(new ListItem(typeof(EventInfo), objEvent.Name));
                }
            }
            else if (objItem.Type.Name == typeof(IComponent).Name)
            {
                IComponent objComponent = this.Components[objItem.Name]; 

                foreach (EventInfo objEvent in objComponent.GetType().GetEvents())
                {
                    cboEvent.Items.Add(new ListItem(typeof(EventInfo), objEvent.Name));
                }
            }
            else if (objItem.Type.Name == "Construct")
            {
                Construct objConstruct = this.Constructs[objItem.Name];
                ClassNode objClass = objConstruct.Node as ClassNode;

                if (objClass != null)
                {
                    foreach (EventNode objEvent in objClass.Events)
                    {
                        cboEvent.Items.Add(new ListItem(typeof(EventNode), objEvent.Name));
                    }
                }
            }
        }

        private void cboEvent_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboStateObject.Enabled = true; 
        }

        private void cboStateObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem objItem = (ListItem)cboStateObject.SelectedItem;

            cboStateMember.Items.Clear();
            cboStateMember.Enabled = true;

            if (objItem.Type.Name == typeof(Form).Name)
            {
                foreach (MemberInfo objMember in typeof(Form).GetMembers())
                {
                    cboStateMember.Items.Add(new ListItem(typeof(MemberInfo), objMember.Name));
                }
            }
            else if (objItem.Type.Name == typeof(UserControl).Name)
            {
                foreach (MemberInfo objMember in typeof(UserControl).GetMembers())
                {
                    cboStateMember.Items.Add(new ListItem(typeof(MemberInfo), objMember.Name));
                }
            }
            else if (objItem.Type.Name == typeof(IComponent).Name)
            {
                IComponent objComponent = this.Components[objItem.Name];

                foreach (EventInfo objMember in objComponent.GetType().GetMembers())
                {
                    cboStateMember.Items.Add(new ListItem(typeof(MemberInfo), objMember.Name));
                }
            }
            else if (objItem.Type.Name == "Construct")
            {
                Construct objConstruct = this.Constructs[objItem.Name];
                ClassNode objClass = objConstruct.Node as ClassNode;
                InterfaceNode objInterface = objConstruct.Node as InterfaceNode;

                if (objClass != null)
                {
                    foreach (PropertyNode objMember in objClass.Properties)
                    {
                        cboStateMember.Items.Add(new ListItem(typeof(PropertyNode), objMember.Name));
                    }

                    foreach (MethodNode objMember in objClass.Methods)
                    {
                        cboStateMember.Items.Add(new ListItem(typeof(MethodNode), objMember.Name));
                    }
                }
                else if (objInterface != null)
                {
                    foreach (InterfacePropertyNode objMember in objInterface.Properties)
                    {
                        cboStateMember.Items.Add(new ListItem(typeof(InterfacePropertyNode), objMember.Name));
                    }

                    foreach (InterfaceMethodNode objMember in objInterface.Methods)
                    {
                        cboStateMember.Items.Add(new ListItem(typeof(InterfaceMethodNode), objMember.Name));
                    }
                }
            }
        }
    }
}
